<?php

namespace App\Http\Middleware;

use Closure;

class CheckTenant
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $uri = $request->getPathInfo();
        $uri = str_replace('/', '', $uri);
        $url = $request->getUri();
        $admin = \Admin::user();
        if ($admin && !$admin->tenant_id && $url != admin_url('tenant/create')) {
            if ($url == admin_url('tenant')) {
                return $next($request);
            }
            return redirect(admin_url('tenant/create'));
        }
        return $next($request);
    }
}
